package cs419.magnusmi.trackitcs;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AdvSearch extends ActionBarActivity {

    EditText Name;
    EditText ID;
    EditText Serial;
    EditText SpecFeat;
    EditText Acc;
    EditText OS;
    Button search;
    Button back;

    String user;
    String API;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adv_search);

        Name = (EditText) findViewById(R.id.nameView);
        ID = (EditText) findViewById(R.id.IDview1);
        Serial = (EditText) findViewById(R.id.serialView);
        SpecFeat = (EditText) findViewById(R.id.specFeatView);
        Acc = (EditText) findViewById(R.id.accessoryView);
        OS = (EditText) findViewById(R.id.osView);

        Intent i = getIntent();
        user = i.getStringExtra("user");
        API = i.getStringExtra("API");

        //search button
        search = (Button) findViewById(R.id.advSearchButton);
        search.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                validate();
            }

        });

        //back button
        back = (Button) findViewById(R.id.backfromsearch);
        back.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                Intent i = new Intent(getApplicationContext(), Inventory.class);
                Utility.addToIntent(i, user, API);
                startActivity(i);
            }
        });
    }

    public void validate() {

        Name.setError(null);

        String ItemName = Name.getText().toString();
        String ItemID = ID.getText().toString();
        String SerialNum = Serial.getText().toString();
        String SpecialFeatures = SpecFeat.getText().toString();
        String Accessories = Acc.getText().toString();
        String OperatingSystem = OS.getText().toString();



        View focusView = null;
        int populated = 6;

        if (TextUtils.isEmpty(ItemName)){
            populated--;
            ItemName = "null";
        }
        if (TextUtils.isEmpty(ItemID)){
            populated--;
            ItemID = "null";
        }
        if (TextUtils.isEmpty(SerialNum)){
            populated--;
            SerialNum = "null";
        }
        if (TextUtils.isEmpty(SpecialFeatures)){
            populated--;
            SpecialFeatures = "null";
        }
        if (TextUtils.isEmpty(Accessories)){
            populated--;
            Accessories = "null";
        }
        if (TextUtils.isEmpty(OperatingSystem)){
            populated--;
            OperatingSystem = "null";
        }

        if (populated == 0){
            Name.setError("At least one field is required");
            focusView = Name;
            focusView.requestFocus();
        }
        else{
            Intent i = new Intent(getApplicationContext(), ASearchActivity.class);
            Utility.addToIntent(i, user, API);
            i.putExtra("ItemName", ItemName);
            i.putExtra("ItemID", ItemID);
            i.putExtra("SerialNum", SerialNum);
            i.putExtra("SpecialFeatures", SpecialFeatures);
            i.putExtra("Accessories", Accessories);
            i.putExtra("OperatingSystem", OperatingSystem);
            startActivity(i);
        }

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.adv_search, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.logout:
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                //clear stack to enforce logout (http://stackoverflow.com/questions/3007998/on-logout-clear-activity-history-stack-preventing-back-button-from-opening-l)
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                        Intent.FLAG_ACTIVITY_CLEAR_TASK |
                        Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
                return true;
            case R.id.inventory_menu:
                Intent j = new Intent(getApplicationContext(), Inventory.class);
                Utility.addToIntent(j, user, API);
                startActivity(j);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }



}
